{{ define "content" }}

<div class="nav-title">
    <div class="float-left fs14"><a href="/">{{.SiteCf.Name}}</a> &raquo; <a href="/n/{{.Cobj.Id}}">{{.Cobj.Name}}</a> ({{.Cobj.Articles}})</div>
    {{if ge .CurrentUser.Flag 5}}
    <div class="float-right"><a href="/newpost/{{.Cobj.Id}}" rel="nofollow" class="newpostbtn">+发新帖</a></div>
    {{end}}
    <div class="c"></div>
</div>

<div class="main-box">
    <div class="topic-title">
        <div class="topic-title-main float-left">
            <h1>{{.Aobj.Title}}</h1>
            <div class="topic-title-date">
                By <a href="/member/{{.Aobj.Uid}}">{{.Aobj.Name}}</a>
                at {{.Aobj.AddTimeFmt}} • {{.Aobj.Views}}次点击

                {{if ge .CurrentUser.Flag 5}}
                {{if not .Aobj.CloseComment}}
                 • <a href="#new-comment">回复</a>
                {{end}}
                {{end}}

                {{if ge .CurrentUser.Flag 99}}
                 • <a href="/admin/post/edit/{{.Aobj.Id}}">编辑</a>
                {{end}}

            </div>
        </div>
        <div class="detail-avatar">
            <a href="/member/{{.Aobj.Uid}}"><img src="/static/avatar/{{.Aobj.Avatar}}.jpg" alt="{{.Aobj.Name}}" /></a></div>
        <div class="c"></div>
    </div>
    <div class="topic-content">

        {{.Aobj.ContentFmt}}

        {{if .Aobj.Tags}}
        <div class="c"></div>
        <br/>
        <p class="mytag float-left">{{.Aobj.TagStr}}</p>
        {{end}}

        <div class="c"></div>
        <br/>

        {{if .Relative.Articles}}
        <div class="has_adv">
            <h3>相关帖子：</h3>
            <ul class="rel_list">
                {{range $_, $item := .Relative.Articles}}
                <li><a href="/t/{{$item.Id}}" title="{{$item.Title}}">{{$item.Title}}</a></li>
                {{end}}

                <div class="c"></div>
            </ul>
            <div class="c"></div>
        </div>
        {{end}}

    </div>

</div>
<!-- post main content end -->

{{if .Aobj.Comments}}
<div class="nav-title">
    共 {{.Aobj.Comments}} 条回复
</div>
<div class="main-box home-box-list">

    {{range $_, $item := .PageInfo.Items}}
    <a name="{{$item.Id}}"></a>
    <div class="commont-item">
        <div class="commont-avatar">
            <a href="/member/{{$item.Uid}}">
                <img src="/static/avatar/{{$item.Avatar}}.jpg" alt="{{$item.Name}}" />
            </a>
        </div>
        <div class="commont-data">
            <div class="commont-content">
                {{$item.ContentFmt}}
            </div>

            <div class="commont-data-date">
                <div class="float-left">
                    <a href="/member/{{$item.Uid}}">{{$item.Name}}</a> at {{$item.AddTimeFmt}}
                    {{if ge $.CurrentUser.Flag 99}}
                    &nbsp;&nbsp;&nbsp; • <a href="/admin/comment/edit/{{$item.Aid}}/{{$item.Id}}">编辑</a>
                    {{end}}
                </div>
                <div class="float-right">
                    {{if ge $.CurrentUser.Flag 5}}
                    {{if not $.Aobj.CloseComment}}
                    {{if ne $.CurrentUser.Id $item.Uid}}
                    &laquo; <a href="#new-comment" onclick="replyto('{{$item.Name}}');">回复</a>
                    {{end}}
                    {{end}}
                    {{end}}
                    <span class="commonet-count">{{$item.Id}}</span>
                </div>
                <div class="c"></div>
            </div>
            <div class="c"></div>
        </div>
        <div class="c"></div>
    </div>
    {{end}}


    <div class="pagination">
        {{if .PageInfo.HasPrev}}
        <a href="/t/{{.Aobj.Id}}?btn=prev&key={{.PageInfo.FirstKey}}" class="float-left">&laquo; 上一页</a>
        {{end}}
        {{if .PageInfo.HasNext}}
        <a href="/t/{{.Aobj.Id}}?btn=next&key={{.PageInfo.LastKey}}" class="float-right">下一页 &raquo;</a>
        {{end}}
        <div class="c"></div>
    </div>

</div>
<!-- comment list end -->

<script type="text/javascript">
    function replyto(somebd){
        var con = document.getElementById("id-content").value;
        document.getElementsByTagName('textarea')[0].focus();
        document.getElementById("id-content").value = " @"+somebd+" " + con;
    }
</script>

{{else}}
<div class="no-comment">目前尚无回复</div>
{{end}}

{{if .Aobj.CloseComment}}
<div class="no-comment">该帖评论已关闭</div>
{{end}}

{{if ge .CurrentUser.Flag 5}}
{{if not .Aobj.CloseComment}}
<a name="new-comment"></a>
<div class="nav-title">
    <div class="float-left">添加一条新回复</div>
    <div class="float-right"><a href="#">&uarr; 回到顶部</a></div>
    <div class="c"></div>
</div>
<div class="main-box">
    <form action="#new-comment" method="POST" enctype="multipart/form-data">
        <p><textarea id="id-content" name="content" class="comment-text mll"></textarea></p>
        <div class="c"></div>

        <div class="float-right grey fs12">
            <input id="file_upload" name="file_upload" type="file" multiple="true">
            <div id="file-queue"></div>
            <div class="c"></div>
        </div>
        <div class="c"></div>

        <p>
            <div class="float-left">
                <input id="btn-preview" type="button" value=" 预 览 " name="submit" class="textbtn" />
                <input id="btn-submit" type="submit" value=" 提 交 " name="submit" class="textbtn" />
            </div>
            <div class="float-right fs12 grey">
                • 请尽量让自己的回复能够对别人有帮助！</div>
            <div class="c"></div>
        </p>
    </form>

    <div id="id_preview" class="commont-content"></div>

</div>

<script>
    jQuery(function($){
        $("#btn-preview").click(function(){
            var content = $("#id-content").val();
            if(content){
                $(this).attr("disabled", true);
                $.ajax({
                    type: "POST",
                    url: "/content/preview",
                    data: JSON.stringify({"act": "preview", "content": content}),
                    dataType: "json",
                    contentType: "application/json",
                    success: function(data){
                        $("#id_preview").html(data.html);
                        $("#id_preview").addClass("previews");
                        $("#id_preview pre code").each(function(i, block) {
                            hljs.highlightBlock(block);
                        });
                        $("#btn-preview").attr("disabled", false);
                    },
                    fail: function(errMsg) {
                        $("#btn-preview").attr("disabled", false);
                    }
                });
            }else{
                $("#id-content").focus();
            }
            return false;
        });

        $("#btn-submit").click(function(){
            var content = $("#id-content").val();
            if(content){
                $(this).attr("disabled", true);
                $.ajax({
                    type: "POST",
                    url: "/t/{{.Aobj.Id}}",
                    data: JSON.stringify({"act": "comment_submit", "content": content}),
                    dataType: "json",
                    contentType: "application/json",
                    success: function(data){
                        if(data.retcode == 200) {
                            location.reload();
                        }
                    },
                    fail: function(errMsg) {
                        $("#btn-preview").attr("disabled", false);
                    }
                });
            }else{
                $("#id-content").focus();
            }
            return false;
        });

        $('#file_upload').uploadifive({
            'auto': true,
            'queueID': 'file-queue',
            'checkScript': false,
            'fileSizeLimit': {{.SiteCf.UploadMaxSizeByte}},
            'multi': true,
            'uploadLimit': 10,
            'queueSizeLimit': 10,
            'buttonText': "上传文件",
            'fileType': false,
            'fileObjName': 'file',
            'removeCompleted': true,
            'truncateLength': 30,
            'height': 22,
            'width': 80,
            'uploadScript': '/file/upload',
            'onUploadComplete': function (file, data) {
                console.log(file);
                var rsp = jQuery.parseJSON(data);
                if(rsp.retcode == 200) {
                    var con = document.getElementById("id-content").value;
                    document.getElementsByTagName("textarea")[0].focus();
                    document.getElementById("id-content").value = con + "\n"+rsp.url+"\n";
                }else{
                    $.toast(rsp.retmsg);
                }
            },
            'onError': function (errorType) {
                $.toast('The error was: ' + errorType);
            }
        });
    });
</script>


<!-- new comment end -->
{{end}}

{{else}}
<div class="no-comment">请 <a href="/login" rel="nofollow">登录</a> 后发表评论</div>
{{end}}


<script>
    $(".topic-content, .commont-content").find("a").click(function(){
        var link = $(this).attr("href");
        if(link.indexOf("/member/") !== 0) {
            $.ajax({
                type: "POST",
                url: "/t/{{.Aobj.Id}}",
                data: JSON.stringify({"act": "link_click", "link": link}),
                dataType: "json",
                success: function(data){
                    console.log(data);
                }
            });
        }
    });
</script>

{{ end}}

